import gradio as gr
import dao

def process_file(uploaded_file, pre_fun):
    print(f"文件上传成功，绝对路径为：{uploaded_file}")
    print(f"选择预处理方式{pre_fun}")
    pre_fig = dao.pre_single_display(uploaded_file, pre_fun)
    #print(pre_fig)
    return pre_fig


with gr.Blocks(css="footer {visibility: hidden}") as webui:
    with gr.Tab("预处理"):
        with gr.Row():
            with gr.Column():
                pre_input = gr.FileExplorer(root_dir='./resources/data',file_count='single',ignore_glob="*.py",container=True,show_label=True,label='选择数据文件')
            with gr.Column():
                pre_change = gr.Checkboxgroup(["SG滤波","归一化","基线校正","异常值剔除"],label="预处理选择",info="可多种预处理同时选择,最后将展现原数据和处理后的数据图片")
                pre_run_buttun = gr.Button(value="预处理")
        with gr.Row():
            pre_plot = gr.Plot()
        pre_run_buttun.click(fn=process_file, inputs=[pre_input, pre_change],outputs=pre_plot)

    with gr.Tab("PLS谱图分析"):
        with gr.Row("数据输入"):
            with gr.Column():
                train_floder_input = gr.FileExplorer(root_dir='./resources/data',ignore_glob="*.py",label="训练集文件夹输入")
            with gr.Column():
                inference_input = gr.FileExplorer(root_dir='./resources/data',ignore_glob="*.py",label="预测文件夹,也可支持单个文件输入")
        with gr.Row("预处理选择"):
            pre_PLS_fun = gr.Checkboxgroup(["SG滤波","归一化","基线校正","异常值剔除"],label="预处理选择",info="可多种预处理同时选择,目前好像不做预处理效果更好")
        with gr.Row():
            with gr.Column():
                pls_n_components_input = gr.Slider(minimum=1,maximum=10,value=3,step=1,label="n_components",info="这个值影响拟合效果")
            with gr.Column():
                pls_run_buttun = gr.Button("开始预测")
        with gr.Row("浓度预测结果"):
                pls_result_label = gr.Label(label="浓度推演",show_label=True,value="浓度推演")
        pls_run_buttun.click(fn=dao.pls,inputs=[train_floder_input,inference_input,pre_PLS_fun,pls_n_components_input],outputs=pls_result_label)

    with gr.Tab("MCR-ALS谱图分离"):
        with gr.Row():
            with gr.Column():
                MCRALS_floder_input = gr.FileExplorer(root_dir='./resources/data', ignore_glob="*.py",label="文件夹输入")
            with gr.Column():
                k_components_input = gr.Slider(minimum=1,maximum=10,value=3,step=1,label="猜测组成数量",info="输入一个预测值，待测物品有多少种物质")
                pre_MCRALS_fun = gr.Checkboxgroup(["SG滤波", "归一化", "基线校正", "异常值剔除"], label="预处理选择",
                                               info="可多种预处理同时选择")
        with gr.Row():
            MCRALS_run_buttun = gr.Button("开始预测")
        with gr.Row("预测结论"):
            with gr.Column("各元素光谱预测"):
                MCRALS_plot = gr.Plot()
            with gr.Column("各样品中各材料的预计浓度"):
                MCRALS_DateFrame = gr.DataFrame()
        MCRALS_run_buttun.click(fn=dao.mcrals,inputs=[MCRALS_floder_input,k_components_input,pre_MCRALS_fun],outputs=[MCRALS_plot,MCRALS_DateFrame])
    css = "footer {visibility: hidden}"






if __name__ == "__main__":
    #webui.queue().launch()
    webui.launch()